package com.newworld.dmsapp2.ui.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.newworld.dmsapp2.R;

/**
 * 自定义dialog
 * 
 * @author Administrator
 * 
 */
// 使用样例
/*
 * final CustomDialog dialog = new CustomDialog(LoginActivity.this);
 * dialog.setTitle("标题"); dialog.setDetial("内容"); dialog.setLeftOnClick(new
 * OnClickListener(){
 * 
 * @Override public void onClick(View v) { dialog.dismiss(); }}); dialog.show();
 */
public class CustomEditSexDialog {
	private TextView title;
	private RadioButton radioButton_man, radioButton_woman;
	private Dialog mDialog;
	private String str;
	private LayoutInflater mInflater;
	private OnStateChagedListener mCallBack;

	// 接口
	public interface OnStateChagedListener {
		void onStateChanged(String str);
	}

	public void setOnStateChagedListener(OnStateChagedListener mCallBack) {
		this.mCallBack = mCallBack;
	}

	public CustomEditSexDialog(Context context) {
		mDialog = new Dialog(context, R.style.customDialog); // 一个无title样式
		mInflater = LayoutInflater.from(context);
		View dialogView = mInflater.inflate(R.layout.custom_edit_sex_dialog,
				null);

		Window window = mDialog.getWindow();
		window.setContentView(dialogView);

		findView(window);

		// RadioGroupListener listener=new RadioGroupListener();
		// radioGroup_sex.setOnCheckedChangeListener(listener);
		radioButton_man.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				str = "男";
				radioButton_man.setChecked(true);
				radioButton_woman.setChecked(false);
				tryNotifyStateChanged();

			}
		});
		radioButton_woman.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				str = "女";
				radioButton_woman.setChecked(true);
				radioButton_man.setChecked(false);
				tryNotifyStateChanged();
			}
		});
	}

	private void findView(Window window) {
		title = (TextView) window
				.findViewById(R.id.custom_edit_sex_dialog_title);
		radioButton_man = (RadioButton) window
				.findViewById(R.id.radioButton_man);
		radioButton_woman = (RadioButton) window
				.findViewById(R.id.radioButton_woman);
	}

	class RadioGroupListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup radio1, int id1) {
			// TODO Auto-generated method stub
			if (id1 == radioButton_man.getId()) {
				// 把mRadio1的内容传到mTextView1
				tryNotifyStateChanged();

			} else if (id1 == radioButton_woman.getId()) {
				// 把mRadio2的内容传到mTextView1
				tryNotifyStateChanged();

			}
		}
	}


	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		this.title.setText(title);
	}

	/**
	 * 设置详情
	 * 
	 * @param detial
	 */
	@SuppressLint("NewApi")
	public void setDetial(String str) {

		this.str = str;
		if (str.equals("男")) {
			radioButton_man.setChecked(true);
			radioButton_woman.setChecked(false);

		} else {
			radioButton_woman.setChecked(true);
			radioButton_man.setChecked(false);
		}
	}

	public String getDetial() {
		return str;
	}

	

	public void show() {
		mDialog.show();
	}

	public void dismiss() {
		mDialog.dismiss();
	}

	private void tryNotifyStateChanged() {
		if (mCallBack != null) {
			mCallBack.onStateChanged(str);
		}
	}
}
